package com.ankiang.treeprint.model;

import com.alibaba.fastjson.JSONObject;
import com.ankiang.treeprint.lib.Pinyin;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

public class TRGroupItem implements Comparable, Serializable {
    private static final String TEXT_NO_NAME = "未命名";
    private static final String TEXT_NO_NOTE = "请更改备注内容";
    int id;
    String name;
    String updatetime;
    String inserttime;
    String viewtime;
    String note;
    String image;
    String pinyinName;

    public String getPinyinName() {
        if(name == null || "".equals(name)){
            return "";
        }
        this.pinyinName = Pinyin.hanziToPinyin(name);
        return pinyinName;
    }

    public TRGroupItem(){
        id = 0;
        name = TEXT_NO_NAME;
        note = TEXT_NO_NOTE;
        String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        updatetime = now;
        inserttime = now;
        viewtime = now;
    }

    public boolean isNameEmpty(){
        if(name == null || "".equals(name)){
            return true;
        }
        if(name.equals(TEXT_NO_NAME)){
            return true;
        }
        return false;
    }

    public boolean isNoteEmpty(){
        if(note == null || "".equals(note)){
            return true;
        }
        if(note.equals(TEXT_NO_NOTE)){
            return true;
        }
        return false;
    }

    public static TRGroupItem convert(JSONObject jobj) {

        TRGroupItem item = new TRGroupItem();
        item.setId(jobj.getInteger("id"));
        item.setName(jobj.getString("name"));
        item.setNote(jobj.getString("note"));
        item.setImage(jobj.getString("image"));
        item.setViewtime(jobj.getString("viewtime"));
        item.setInserttime(jobj.getString("inserttime"));
        item.setUpdatetime(jobj.getString("updatetime"));
        item.setImage(jobj.getString("image"));
        return item;
    }

    public static List<TRGroupItem> sort(List<TRGroupItem> gList) {
        if(gList == null){
            return gList;
        }
        Collections.sort(gList, new Comparator<TRGroupItem>() {
            @Override
            public int compare(TRGroupItem o2, TRGroupItem o1) {
                if (o1 == null || o1.getUpdatetime() == null) {
                    return 1;
                }

                if (o2 == null || o2.getUpdatetime() == null) {
                    return -1;
                }

                return o1.getId()-o2.getId();
            }
        });
        return gList;

    }

    public static List<TRGroupItem> sortReverse(List<TRGroupItem> gList){
        if(gList == null){
            return gList;
        }
        Collections.sort(gList, new Comparator<TRGroupItem>() {
            @Override
            public int compare(TRGroupItem o1, TRGroupItem o2) {
                if (o1 == null || o1.getUpdatetime() == null) {
                    return 1;
                }

                if (o2 == null || o2.getUpdatetime() == null) {
                    return -1;
                }

                return o1.getId()-o2.getId();
            }
        });
        return gList;

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUpdatetime() {
        return updatetime;
    }

    public void setUpdatetime(String updatetime) {
        this.updatetime = updatetime;
    }

    public String getInserttime() {
        return inserttime;
    }

    public void setInserttime(String inserttime) {
        this.inserttime = inserttime;
    }

    public String getViewtime() {
        return viewtime;
    }

    public void setViewtime(String viewtime) {
        this.viewtime = viewtime;
    }

    public JSONObject toJSONObject(){

        JSONObject jobj = new JSONObject();
        jobj.put("id", this.getId());
        jobj.put("name", this.getName());
        jobj.put("updatetime", this.getUpdatetime());
        jobj.put("inserttime", this.getInserttime());
        jobj.put("viewtime", this.getViewtime());
        jobj.put("image", this.getImage());
        jobj.put("note", this.getNote());
        return jobj;
    }

    public String getNote() {
        return note;
    }
    public String getImage() {
        return image;
    }
    public void setNote(String note){
        this.note = note;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public void refreshUpdatetime() {
        String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        this.setUpdatetime(now);
        this.setViewtime(now);
    }

    @Override
    public int compareTo(Object o) {
        TRGroupItem other = (TRGroupItem)o;
        if (o==null){
            return 1;
        }
        if(other.getId()!=this.getId()){
            return other.getId() - this.getId();
        }
        if(other.getName()!=this.getName()){
            return other.getName().compareTo(this.getName());
        }
        if(other.getUpdatetime()!=this.getUpdatetime()){
            if(other.getName() == null){
                return -1;
            }
            return other.getName().compareTo(this.getName());
        }
        if(other.getNote()!=this.getNote()){

            if(other.getName() == null){
                return -1;
            }
            return other.getName().compareTo(this.getName());
        }
        if(other.getImage()!=this.getImage()){

            if(other.getName() == null){
                return -1;
            }
            return other.getName().compareTo(this.getName());
        }
        if(other.getInserttime()!=this.getInserttime()){
            if(other.getName() == null){
                return -1;
            }
            return other.getName().compareTo(this.getName());
        }
        return 0;
    }
}
